package com.dede.net_speed_monitor.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.util.Log;

import com.dede.net_speed_monitor.util.NetUtil;

public class NetChangeBroadcastReceiver extends BroadcastReceiver {

    public final String TAG = NetChangeBroadcastReceiver.class.getSimpleName();

    private OnNetChangeListener mNetChangeListener;

    public void setNetChangeListener(OnNetChangeListener netChangeListener) {
        mNetChangeListener = netChangeListener;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "onReceive: " + intent.getAction());
        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
            if (mNetChangeListener != null)
                mNetChangeListener.onNetChange(NetUtil.getNetState(context));
        }
    }

    public interface OnNetChangeListener {

        /**
         * 网络状态发生改变时回调的方法
         *
         * @param state 网络状态
         */
        void onNetChange(@NetUtil.NetType int state);
    }
}
